package com.rc.evcharger.netty.listener;

import com.rc.evcharger.redis.RedisService;
import com.rc.evcharger.netty.server.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.util.Properties;

/**
 * netty监听器,需要在web.xml加载启动
 */
public class NettyListener implements ServletContextListener {
    Logger logger = LoggerFactory.getLogger(NettyListener.class);
    private static WebApplicationContext springContext;

    private RedisService redisService;

    private String JZ_DOMAIN_NETTY_PROT = null;

//    private final String filePath = "/var/wwwroot-stdevcharger/application-pro.properties";
    //private final String filePath = "d:\\wwwroot-stdevcharger/application-pro.properties";

    /**
     * 在listener里启动netty服务,不能再主线程里面启动，在主线程启动会阻塞 tomcat的启动，需要new 一个线程里面启动
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        String JZ_DOMAIN_NETTY_PROT = null;
        String applicationName = null;

        //Resource resource = new ClassPathResource("/application-pro.properties");
        //Resource resource = new ClassPathResource("D://application-pro.properties");


        try {
        Properties p = new Properties();
        p = PropertiesLoaderUtils.loadAllProperties("application-custom.properties");
        JZ_DOMAIN_NETTY_PROT = p.getProperty("JZ_DOMAIN_NETTY_PROT");
//
//
//            //JZ_DOMAIN_NETTY_PROT = (String) PropertiesLoaderUtils.loadProperties(resource).get("JZ_DOMAIN_NETTY_PROT");
        } catch (IOException e) {
            logger.error("找不到文件名称", e.getMessage());
            JZ_DOMAIN_NETTY_PROT = "9999";
//
        }
        logger.warn("nettyListener Startup!,启动端口号:{}", JZ_DOMAIN_NETTY_PROT);
        String finalJZ_DOMAIN_NETTY_PROT = JZ_DOMAIN_NETTY_PROT;
        new Thread() {
            @Override
            public void run() {
                try {
                    new NettyServer(Integer.valueOf(finalJZ_DOMAIN_NETTY_PROT)).run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        springContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        System.out.println("afe");
    }

    public static ApplicationContext getApplicationContext() {
        return springContext;
    }

    /**
     * 关闭服务时将所有桩设成离线
     * 并且将挂起的订单进行保存到redis中
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //将所有桩的登录状态设为离线
        logger.info("销毁进程，关闭服务");
        //ElecPileService elecPileService = (ElecPileService) springContext.getBean("elecPileService");
        //elecPileService.updatePileOffLine();
        redisService.putExOrder();
    }
}
